<?php
/**
 * Oss.php
 * phpstorm_company_name_str - phpstorm_company_product_name
 * ***************************************************************************************************************************
 * 版权声明: Copyright ©2019 phpstorm_company_name_str All rights reserved
 * 官方网站: phpstorm_company_product_url
 * 许可声明: 这不是一个自由软件！您只能在购买源代码后或者经 phpstorm_company_name_str 官方授权的前提下对程序代码进行修改和使用，
 * 任何企业或个人都不得在源代码上以任何形式任何目的的再次发布和销售以及镜像或复制,包括但不限于已购买源代码和官方授权者。
 * 警 告  : 如若您或贵司未经正式购买渠道获得授权，请立即停止侵权行为，我们已经委派 phpstorm_company_auth_legal 作为我们侵权
 * 起诉的法律代表，还请您或贵司尊重商业版权以及劳动成果，自觉停止侵权行为。您也可匿名举报侵权行为，一旦您的举报被证实，我们
 * 将会以本版本零售价的 phpstorm_company_product_percent_money% 现金给您，以便作为您的辛苦奖励。同时，我们也将竭尽全力保证您的隐私安全。举报邮箱: phpstorm_company_product_report_mail
 * 协议约定: 本产品遵守 << phpstorm_company_product_protocol >> 的约束,您或贵司也可以访问 phpstorm_company_product_protocol 了解更多详情信息。
 * 致 谢  : 感谢您或贵司对我们产品的支持和热爱，我们将不定期对本产品进行更新和修复bug。还请您和贵司多多指导。更多问题资讯请访问
 * phpstorm_company_product_help_url ,如果您正在对此版本进行二次开发，提bug或者需求可以发送到@author邮箱,或者提交工单。
 * 工单地址是: phpstorm_company_product_ticket_url 我们将尽快为您提出合理的解决方案。
 * 其 他  : 您或贵司也可以登录 phpstorm_company_product_doc_url 查看相关的文档资料以及FAQ！
 * ***************************************************************************************************************************
 * @product ctmoncms
 * @desc
 * @author justary <justary@126.com>
 * @date 2024/06/25 11:48
 * @version phpstorm_company_product_version
 * @copyright phpstorm_company_product_copyright
 */

/**
 * @package app\data\components
 *
 * @filesource Oss.php
 * @author justary <justary@126.com>
 * @date 2024/06/25 11:48
 * @link phpstorm_product_licence
 * @version phpstorm_company_product_version
 * @copyright phpstorm_company_product_copyright
 */


namespace app\data\components;


use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;
use OSS\Model\ObjectListInfo;
use OSS\OssClient;
use think\facade\Env;
use think\facade\Log;

class Oss
{
    /**
     * @var OssClient
     */
    public OssClient $ossClient;

    public function __construct($id, $secret, $endpoint)
    {
        try {
            $this->ossClient = new OssClient($id, $secret, $endpoint);
        } catch (\OssException $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }
    }

    /**
     * subject content
     *
     * @param $bucket
     * @param $object
     * @param $content
     * @return array|void
     * @author justary  <justary@126.com>
     * @date 2024-06-25 11:59
     */
    public function put($bucket, $object, $content)
    {
        try {
            $ret = $this->ossClient->putObject($bucket, $object, $content);
            return ['state' => true, 'msg' => 'SUCCESS', 'data' => $ret];
        } catch (OssException|RequestCore_Exception $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }
    }

    public function upload($bucket, $object, $filePath): array
    {
        try {
            $ret = $this->ossClient->uploadFile($bucket, $object, $filePath);
            return ['state' => true, 'msg' => 'SUCCESS', 'data' => $ret];
        } catch (OssException|RequestCore_Exception $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }
    }

    /**
     * subject content
     *
     * @param $bucket
     * @return array|void
     * @author justary  <justary@126.com>
     * @date 2024-06-25 12:00
     */
    public function createBucket($bucket)
    {
        try {
            $ret = $this->ossClient->createBucket($bucket);
            return ['state' => true, 'msg' => 'SUCCESS', 'data' => $ret];
        } catch (OssException|RequestCore_Exception $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }
    }

    /**
     * subject content
     *
     * @throws OssException
     * @throws RequestCore_Exception
     * @author justary  <justary@126.com>
     * @date 2024-06-25 13:20
     */
    public function listAll()
    {
        $bucketListInfo = $this->ossClient->listBuckets();
        $bucketList = $bucketListInfo->getBucketList();
        $data = [];
        foreach ($bucketList as $bucket) {
            //print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreateDate() . "\n");
            $data[]['content'] = [
                'url' => $bucket->getLocation(),
                'name' => $bucket->getName(),
                'create_time' => $bucket->getCreateDate()
            ];
        }
        return ['state' => true, 'msg' => 'SUCCESS', 'data' => $data];
    }

    /**
     * subject content
     *
     * @param string $bucket
     * @return array
     * @author justary  <justary@126.com>
     * @date 2024-06-25 14:28
     */
    public function listByBucket(string $bucket = ''): array
    {
        try {
            $list = $this->ossClient->getBucketCname($bucket);
            return ['state' => true, 'msg' => 'SUCCESS', 'data' => $list];
        } catch (OssException|RequestCore_Exception $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }

    }


    /**
     * 所有的文件对象
     * @param string $bucket
     */
    public function listByObjects(string $bucket, string $prefix)
    {
        $option = array(
            OssClient::OSS_MARKER => null,
            // 填写待删除目录的完整路径，完整路径中不包含Bucket名称。
            OssClient::OSS_PREFIX => $prefix,
            OssClient::OSS_DELIMITER => '',
        );
        $list = $this->ossClient->listObjects($bucket, $option);
        return $list;
    }

    /**
     * 移除文件对象
     * @param array $bucket
     * @param string $object
     * @return array
     */
    public function deleteByObjects(string $bucket, array $object)
    {
        try {
            $list = $this->ossClient->deleteObjects($bucket, $object);
            return ['state' => true, 'msg' => 'SUCCESS', 'data' => $list];
        } catch (OssException|RequestCore_Exception $e) {
            return ['state' => false, 'msg' => $e->getMessage(), 'tag' => 'OSS_ERROR'];
        }
    }

}
